package com.huawei.hwCloudJs.service.hms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.Api.ApiOptions.NoOptions;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.api.HuaweiApiClientImpl;
import com.huawei.hms.support.api.client.PendingResult;
import com.huawei.hms.support.api.client.SubAppInfo;
import com.huawei.hms.support.api.entity.auth.PermissionInfo;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.LOCALPERMISSION;
import com.huawei.hms.support.api.hwid.HuaweiId;
import com.huawei.hms.support.api.hwid.HuaweiIdSignInOptions.Builder;
import com.huawei.hms.support.api.hwid.SignInResult;
import com.huawei.hwCloudJs.annotation.JSMethod;
import com.huawei.hwCloudJs.core.JSRequest;
import com.huawei.hwCloudJs.core.JsCallback;
import com.huawei.hwCloudJs.core.c;
import com.huawei.hwCloudJs.service.hms.a.b;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class HmsCoreApi extends JSRequest implements c {
    private static final String a = "HmsCoreApi";
    private static final int b = 9001;
    private static final int c = 9002;
    private static final Api<NoOptions> d = new Api(HuaweiApiAvailability.HMS_API_NAME_PAY);

    private HuaweiApiClient a(Context context, boolean z, List<Scope> list) {
        Builder builder = new Builder();
        if (z) {
            builder.requestServerAuthCode();
        } else {
            builder.requestAccessToken().requestOpenId();
        }
        HuaweiApiClient.Builder builder2 = new HuaweiApiClient.Builder(context.getApplicationContext());
        builder2.addApi(HuaweiId.SIGN_IN_API, builder.build());
        builder2.addApi(d);
        if (list != null) {
            for (Scope addScope : list) {
                builder2.addScope(addScope);
            }
        }
        return builder2.build();
    }

    private static List<Scope> a(String str) {
        List<Scope> arrayList = new ArrayList();
        if (TextUtils.isEmpty(str)) {
            Log.i(a, "JS scopeEmpty");
        } else {
            for (String str2 : str.split(HwAccountConstants.KEY_COMMA)) {
                if (!TextUtils.isEmpty(str2)) {
                    arrayList.add(new Scope(str2));
                }
            }
        }
        return arrayList;
    }

    private void a(SignInResult signInResult, boolean z, WeakReference<Activity> weakReference, WeakReference<HuaweiApiClient> weakReference2, boolean z2, JsCallback jsCallback) {
        if (signInResult.isSuccess()) {
            Log.i(a, "JS signIn result isSuccess");
            jsCallback.success(b.a(signInResult, z));
            return;
        }
        int statusCode = signInResult.getStatus().getStatusCode();
        Log.e(a, "JS signIn result statusCode" + statusCode);
        if (statusCode == 2001 || statusCode == 2004) {
            Activity activity = (Activity) weakReference.get();
            if (activity == null) {
                Log.e(a, "JS signIn webViewActivity == null");
                jsCallback.failure();
                return;
            }
            a(signInResult, z, (WeakReference) weakReference, (WeakReference) weakReference2, z2, jsCallback, activity);
        } else if (statusCode == 2002) {
            Log.i(a, "JS signIn SIGN_IN_AUTH");
            Activity activity2 = (Activity) weakReference.get();
            if (activity2 == null) {
                jsCallback.failure();
                return;
            }
            a.a().a(activity2, signInResult.getData(), 9002, new a(jsCallback, z));
        } else {
            jsCallback.failure(signInResult.getStatus().getStatusCode());
        }
    }

    private void a(SignInResult signInResult, boolean z, WeakReference<Activity> weakReference, WeakReference<HuaweiApiClient> weakReference2, boolean z2, JsCallback jsCallback, Activity activity) {
        a.a().a(activity, signInResult.getData(), 9001, new 3(this, weakReference2, jsCallback, weakReference, z, z2));
    }

    private void a(LoginReq loginReq, JsCallback jsCallback, WebView webView) {
        if (webView == null) {
            Log.e(a, "JS signIn webview is null");
            jsCallback.failure("webview is null");
        } else if (loginReq == null) {
            Log.e(a, "JS signIn JS_RET_CODE_PARSE_PARAM_ERROR");
            jsCallback.failure(13);
        }
    }

    private void a(StartIntentReq startIntentReq, JsCallback jsCallback, WebView webView, String str, String str2) {
        int access$800 = StartIntentReq.access$800(startIntentReq);
        if (access$800 == 1001) {
            Activity activity = (Activity) webView.getContext();
            Intent a = d.a().a(str);
            if (a == null) {
                Log.e(a, "JS hmsInvoke startResolutionForResult intent is null");
                jsCallback.failure("intent is null");
                return;
            }
            try {
                activity.startActivity(a);
            } catch (Exception e) {
                Log.e(a, "JS hmsInvoke startResolutionForResult can not intent this");
                jsCallback.failure("can not intent this");
            }
        } else if (access$800 == 1002 || access$800 == 1003) {
            a(startIntentReq, jsCallback, webView, str, str2, access$800);
        } else if (access$800 == 1004) {
            Intent a2 = d.a().a(str);
            if (a2 == null) {
                Log.e(a, "JS hmsInvoke TYPE_GETDATA_FORRESULT can not intent this");
                jsCallback.failure("intent is null");
            } else if (TextUtils.isEmpty(a2.getAction()) && a2.getComponent() == null) {
                JSONObject jSONObject = new JSONObject();
                Bundle extras = a2.getExtras();
                if (extras != null) {
                    f.a(2).a(str2, extras, jSONObject);
                }
                jsCallback.success(jSONObject.toString());
            } else {
                Log.e(a, "JS hmsInvoke TYPE_GETDATA_FORRESULT it is not data intent");
                jsCallback.failure("it is not data intent");
            }
        } else {
            Log.e(a, "JS hmsInvoke startResolutionForResult failure!");
            jsCallback.failure();
        }
    }

    private void a(StartIntentReq startIntentReq, JsCallback jsCallback, WebView webView, String str, String str2, int i) {
        b.a(str2, jsCallback);
        Activity activity = (Activity) webView.getContext();
        if (i == 1002) {
            Intent a = d.a().a(str);
            if (a == null) {
                Log.e(a, "JS hmsInvoke startResolutionForResult intent == null");
                jsCallback.failure("intent is null");
                return;
            }
            try {
                activity.startActivityForResult(a, 9003);
                return;
            } catch (Exception e) {
                Log.e(a, "JS hmsInvoke startResolutionForResult intentcan not intent this");
                jsCallback.failure("can not intent this");
                return;
            }
        }
        PendingIntent b = d.a().b(str);
        if (b == null) {
            Log.e(a, "JS hmsInvoke intent is null");
            jsCallback.failure("intent is null");
            return;
        }
        Intent intent = null;
        Object access$900 = StartIntentReq.access$900(startIntentReq);
        if (!TextUtils.isEmpty(access$900) && access$900.startsWith(str2)) {
            intent = d.a().a(str);
        }
        try {
            activity.startIntentSenderForResult(b.getIntentSender(), 9003, intent, StartIntentReq.access$1000(startIntentReq), StartIntentReq.access$1100(startIntentReq), StartIntentReq.access$1200(startIntentReq));
        } catch (SendIntentException e2) {
            Log.e(a, "JS hmsInvoke REQUEST_CODE_INVOKE can not intent this");
            jsCallback.failure("can not intent this");
        }
    }

    private void a(b bVar, WeakReference<HuaweiApiClient> weakReference, JsCallback jsCallback, WeakReference<Activity> weakReference2, boolean z, boolean z2) {
        if (bVar.b() == -1) {
            Log.i(a, "JS signIn Activity.RESULT_OK");
            a((WeakReference) weakReference, jsCallback, (WeakReference) weakReference2, z, z2);
            return;
        }
        Log.e(a, "JS signIn JS_RET_CODE_USER_DENAL");
        jsCallback.failure(1);
    }

    private void a(WeakReference<HuaweiApiClient> weakReference, JsCallback jsCallback, WeakReference<Activity> weakReference2, boolean z, boolean z2) {
        HuaweiApiClient huaweiApiClient = (HuaweiApiClient) weakReference.get();
        if (huaweiApiClient == null) {
            jsCallback.failure();
            return;
        }
        PendingResult signInBackend;
        if (z2) {
            signInBackend = HuaweiId.HuaweiIdApi.signInBackend(huaweiApiClient);
        } else {
            Activity activity = (Activity) weakReference2.get();
            if (activity != null) {
                signInBackend = HuaweiId.HuaweiIdApi.signIn(activity, huaweiApiClient);
            } else {
                Log.e(a, "signIn with null activity");
                jsCallback.failure();
                return;
            }
        }
        signInBackend.setResultCallback(new 2(this, z, weakReference2, weakReference, z2, jsCallback));
    }

    public String getApi() {
        return "hms";
    }

    @JSMethod(isOpen = true, name = "hmsInvoke")
    public void hmsInvoke(InvokeReq invokeReq, JsCallback jsCallback) {
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            jsCallback.failure();
        } else if (invokeReq == null) {
            Log.e(a, "JS hmsInvoke req JS_RET_CODE_PARSE_PARAM_ERROR");
            jsCallback.failure(13);
        } else {
            String access$500 = InvokeReq.access$500(invokeReq);
            if (TextUtils.isEmpty(access$500)) {
                Log.e(a, "JS hmsInvoke URI JS_RET_CODE_PARSE_PARAM_ERROR");
                jsCallback.failure(13);
                return;
            }
            JSONObject jSONObject;
            if (TextUtils.isEmpty(InvokeReq.access$600(invokeReq))) {
                jSONObject = new JSONObject();
            } else {
                try {
                    jSONObject = new JSONObject(InvokeReq.access$600(invokeReq));
                } catch (JSONException e) {
                    Log.e(a, "JS hmsInvoke JSONException");
                    jsCallback.failure(13);
                    return;
                }
            }
            String appId = invokeReq.getAppId();
            HuaweiApiClient a = c.a().a(invokeReq.getBridgeId(), appId);
            if (a == null) {
                a = new HuaweiApiClient.Builder(webView.getContext().getApplicationContext()).addApi(HuaweiId.SIGN_IN_API, new Builder().requestOpenId().build()).addApi(d).build();
                a.setSubAppInfo(new SubAppInfo(appId));
            }
            HuaweiApiClient huaweiApiClient = a;
            WeakReference weakReference = new WeakReference(huaweiApiClient);
            Activity activity = (Activity) webView.getContext();
            WeakReference weakReference2 = new WeakReference(activity);
            if (huaweiApiClient.isConnected()) {
                b.a(weakReference, access$500, jSONObject, jsCallback);
                return;
            }
            huaweiApiClient.setConnectionCallbacks(new b.c(weakReference, jsCallback, access$500, jSONObject));
            huaweiApiClient.setConnectionFailedListener(new b.b(weakReference, jsCallback, weakReference2));
            huaweiApiClient.connect(activity);
        }
    }

    public void onCompletedConfig(Context context, String str, String str2, String str3, JSONObject jSONObject) {
        String optString;
        boolean z = false;
        if (jSONObject != null) {
            optString = jSONObject.optString("scope", null);
            z = jSONObject.optBoolean("needAuthCode", false);
        } else {
            optString = null;
        }
        List a = a(optString);
        if (!a.isEmpty()) {
            HuaweiApiClient a2 = a(context, z, a);
            a2.setSubAppInfo(new SubAppInfo(str));
            if (context instanceof Activity) {
                a2.connect((Activity) context);
            } else {
                a2.connect(null);
            }
            c.a().a(str3, str, a2);
        }
    }

    public void onDestroy(String str) {
        c.a().a(str);
    }

    @JSMethod(isOpen = true, name = "signIn")
    public void signIn(LoginReq loginReq, JsCallback jsCallback) {
        boolean z = false;
        WebView webView = jsCallback.getWebView();
        a(loginReq, jsCallback, webView);
        String appId = loginReq.getAppId();
        String bridgeId = loginReq.getBridgeId();
        List a = a(LoginReq.access$000(loginReq));
        boolean isEmpty = a.isEmpty();
        boolean access$100 = LoginReq.access$100(loginReq);
        HuaweiApiClient a2 = c.a().a(bridgeId, appId);
        boolean z2;
        if (a2 != null) {
            boolean a3 = b.a(a, ((HuaweiApiClientImpl) a2).getScopes());
            List<PermissionInfo> permissionInfos = ((HuaweiApiClientImpl) a2).getPermissionInfos();
            if (permissionInfos != null) {
                for (PermissionInfo permissionInfo : permissionInfos) {
                    if (permissionInfo != null && LOCALPERMISSION.SERVICE_AUTH_CODE.equals(permissionInfo.getPermission())) {
                        z = true;
                        z2 = a3;
                        break;
                    }
                }
            }
            z2 = a3;
        } else {
            z2 = false;
        }
        if (!(a2 != null && r0 && r3 == access$100)) {
            if (a2 != null) {
                a2.disconnect();
            }
            a2 = a(webView.getContext().getApplicationContext(), access$100, a);
            a2.setSubAppInfo(new SubAppInfo(appId));
            c.a().a(bridgeId, appId, a2);
        }
        HuaweiApiClient huaweiApiClient = a2;
        Activity activity = (Activity) webView.getContext();
        WeakReference weakReference = new WeakReference(huaweiApiClient);
        WeakReference weakReference2 = new WeakReference(activity);
        if (huaweiApiClient.isConnected()) {
            a(weakReference, jsCallback, weakReference2, access$100, isEmpty);
            return;
        }
        huaweiApiClient.setConnectionCallbacks(new 1(this, weakReference, jsCallback, weakReference2, access$100, isEmpty));
        huaweiApiClient.setConnectionFailedListener(new b.b(weakReference, jsCallback, weakReference2));
        huaweiApiClient.connect(activity);
    }

    @JSMethod(isOpen = true, name = "startResolutionForResult")
    public void startResolutionForResult(StartIntentReq startIntentReq, JsCallback jsCallback) {
        Log.i(a, "JS hmsInvoke startResolutionForResult");
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            jsCallback.failure();
        } else if (startIntentReq == null) {
            Log.e(a, "JS hmsInvoke startResolutionForResult req null");
            jsCallback.failure(13);
        } else {
            String access$700 = StartIntentReq.access$700(startIntentReq);
            if (TextUtils.isEmpty(access$700)) {
                Log.e(a, "JS hmsInvoke startResolutionForResult intentId null");
                jsCallback.failure(13);
                return;
            }
            String appId = startIntentReq.getAppId();
            if (access$700.startsWith(appId)) {
                a(startIntentReq, jsCallback, webView, access$700, appId);
                return;
            }
            Log.e(a, "JS hmsInvoke startResolutionForResult invalid intent id");
            jsCallback.failure("invalid intent id");
        }
    }
}
